package com.framework.util;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Random;
import java.util.UUID;

/**
 * @Description: system util
 * @Author: xiaoche
 * @Date: 2021/11/12 10:24
 */
@Slf4j
public class SystemUtil {

    public static long getId() {
        return SnowflakeIdWorker.generateId();
    }

    public static String getIdStr() {
        return String.valueOf(getId());
    }

    public static String getUUID() {
        String uuId = UUID.randomUUID().toString().replace("-", "");
        return uuId;
    }
    /**
     * 本地ip
     */
    public static final String LOCAL_IP = getLocalIp();

    /**
     * 获取本机ip地址
     * 此方法为重量级的方法，不要频繁调用
     */
    public static String getLocalIp(){
        try{
            //根据网卡取本机配置的IP
            Enumeration<NetworkInterface> netInterfaces=NetworkInterface.getNetworkInterfaces();
            String ip = null;
            a: while(netInterfaces.hasMoreElements()){
                NetworkInterface ni=netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress ipObj = ips.nextElement();
                    if (ipObj.isSiteLocalAddress()) {
                        ip =  ipObj.getHostAddress();
                        break a;
                    }
                }
            }
            return ip;
        }catch (Exception e){
            log.error("getLocalIp fail", e);
            return null;
        }
    }

    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }

    public static String getClientIP(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    private static final Random RANDOM = new Random(System.currentTimeMillis());
    private static final String[] BASIC_CODE_VAL = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public static String getRandomCode(int len) {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            sb.append(BASIC_CODE_VAL[RANDOM.nextInt(BASIC_CODE_VAL.length)]);
        }
        return sb.toString();
    }
}
